package co.yixiang.modules.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import co.yixiang.api.YshopException;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.modules.product.domain.YxBusinessTitle;
import co.yixiang.modules.product.service.YxBusinessTitleService;
import co.yixiang.modules.product.service.dto.BusinessTitleDto;
import co.yixiang.modules.product.service.dto.YxBusinessTitleDto;
import co.yixiang.modules.product.service.dto.YxBusinessTitleQueryCriteria;
import co.yixiang.modules.product.service.mapper.YxBusinessTitleMapper;
import co.yixiang.utils.RegexUtil;
import lombok.AllArgsConstructor;
import co.yixiang.dozer.service.IGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import co.yixiang.domain.PageResult;
/**
* @author lsh
* @date 2021-06-21
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "yxBusinessTitle")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxBusinessTitleServiceImpl extends BaseServiceImpl<YxBusinessTitleMapper, YxBusinessTitle> implements YxBusinessTitleService {

    private final IGenerator generator;

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(YxBusinessTitleQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YxBusinessTitle> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), YxBusinessTitleDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<YxBusinessTitle> queryAll(YxBusinessTitleQueryCriteria criteria){
        List<YxBusinessTitle> yxStoreProductList = baseMapper.selectList(QueryHelpPlus.getPredicate(YxBusinessTitle.class, criteria));
        return yxStoreProductList;

    }


    @Override
    public void download(List<YxBusinessTitleDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxBusinessTitleDto yxBusinessTitle : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("标题图片", yxBusinessTitle.getImage());
            map.put("轮播图", yxBusinessTitle.getSliderImage());
            map.put("标题名称", yxBusinessTitle.getTitleName());
            map.put("标题简介", yxBusinessTitle.getTitleInfo());
            map.put("关键字", yxBusinessTitle.getKeyword());
            map.put("分类id", yxBusinessTitle.getBusinessTypeId());
            map.put("排序", yxBusinessTitle.getSort());
            map.put("销量", yxBusinessTitle.getSales());
            map.put("标题描述", yxBusinessTitle.getDescription());
            map.put("添加时间", yxBusinessTitle.getCreateTime());
            map.put(" updateTime",  yxBusinessTitle.getUpdateTime());
            map.put("是否删除", yxBusinessTitle.getIsDel());
            map.put("浏览量", yxBusinessTitle.getBrowse());
            map.put("级别解锁", yxBusinessTitle.getSingular());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void insertAndEditYxStoreBusinessTitle(BusinessTitleDto businessTitleDto) {
        businessTitleDto.setDescription(RegexUtil.converProductDescription(businessTitleDto.getDescription()));

        //添加商品
        YxBusinessTitle yxBusinessTitle = new YxBusinessTitle();
        BeanUtil.copyProperties(businessTitleDto, yxBusinessTitle, "sliderImage");
        if (businessTitleDto.getSliderImage().isEmpty()) {
            throw new YshopException("请上传轮播图");
        }

        yxBusinessTitle.setSliderImage(String.join(",", businessTitleDto.getSliderImage()));
        this.saveOrUpdate(yxBusinessTitle);



    }


}
